package com.tss.util;

import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class JsendMail
{
	public static class SMTPAuthenticator extends Authenticator
	{
		private String username;
		private String password;

		public SMTPAuthenticator(String username, String password)
		{
			super();
			this.username = username;
			this.password = password;
		}

		protected  PasswordAuthentication getPasswordAuthentication()
		{
			return new PasswordAuthentication(this.username, this.password);
		}
	}
	
	Vector tov;
	String from;
	String host;
	String filename;
	String messagetext;
	String subject;
	String mimeType;
	boolean debug;
	Vector vfile;
	Vector vmsg;
	String auth_user="";
	String auth_pwd="";

	public JsendMail()
	{
		tov = new Vector();
		from = "";
		host = "";
		filename = "";
		messagetext = "";
		subject = "";
		mimeType = "";
		debug = false;
		vfile = new Vector(10, 10);
		vmsg = new Vector(10, 10);
	}

	public JsendMail(String from, String smtpServer, String subject)
	{
		tov = new Vector();
		this.from = "";
		host = "";
		filename = "";
		messagetext = "";
		this.subject = "";
		mimeType = "";
		debug = false;
		vfile = new Vector(10, 10);
		vmsg = new Vector(10, 10);
		this.from = from;
		host = smtpServer;
		this.subject = subject;
	}

	public void attachfile(String fname)
	{
		vfile.addElement(fname);
	}

	public void setMessage(String msg)
	{
		vmsg.addElement(msg);
	}

	public void setDebug(boolean debug)
	{
		this.debug = debug;
	}

	public void setAuthInfo(String user,String pwd)
	{
		this.auth_user	=user;
		this.auth_pwd	=pwd;
	}

	
	public void setMimeType(String mimeType)
	{
		this.mimeType = mimeType;
	}

	public void setTo(Vector to)
	{
		tov = to;
	}

	public void startSend()
		throws Exception
	{
		Properties props = System.getProperties();
		props.put("mail.smtp.host", host);
		Session session = null;
		if (auth_user!=null&&!auth_user.trim().equals(""))
		{
			props.put("mail.smtp.auth", "true");//�����ʼ�ʱ�Ƿ���Ҫ��֤���
			SMTPAuthenticator authenticator = new SMTPAuthenticator(auth_user,auth_pwd);
			session=Session.getInstance(props, authenticator);
		}
		else
		{
			session=Session.getDefaultInstance(props, null);
		}
		session.setDebug(debug);
		try
		{
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setSubject(subject);
			Multipart mp = new MimeMultipart();
			MimeBodyPart mbp1;
			for(Enumeration emsg = vmsg.elements(); emsg.hasMoreElements(); mp.addBodyPart(mbp1))
			{
				messagetext = emsg.nextElement().toString();
				mbp1 = new MimeBodyPart();
				if(mimeType.equals(""))
				{
					mbp1.setText(messagetext,"text/plain");
				}
				else
					mbp1.setContent(messagetext, mimeType);
			}

			vmsg.removeAllElements();
			/*MimeBodyPart mbp2;
			for(Enumeration efile = vfile.elements(); efile.hasMoreElements(); mp.addBodyPart(mbp2))
			{
				mbp2 = new MimeBodyPart();
				filename = efile.nextElement().toString();
				FileDataSource fds = new FileDataSource(filename);
				mbp2.setDataHandler(new DataHandler(fds));
				mbp2.setFileName(fds.getName());
			}*/

			vfile.removeAllElements();
			msg.setContent(mp);
			msg.setSentDate(new Date());
			for(int i = 0; i < tov.size(); i++)
				if(!tov.elementAt(i).equals(""))
				{
					InternetAddress address[] = {
						new InternetAddress(tov.elementAt(i).toString())
					};
					msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
					Transport.send(msg);
				}

		}
		catch(MessagingException mex)
		{
			throw mex;
		}
	}
}
